<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手风琴菜单示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        .accordion {
            width: 100%;
            max-width: 300px;
            margin: 20px auto;
            background-color: #f7f7f7;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .accordion-item {
            border-bottom: 1px solid #ddd;
        }

        .accordion-item:last-child {
            border-bottom: none;
        }

        .accordion-header {
            background-color: #f1f1f1;
            color: #444;
            cursor: pointer;
            padding: 18px;
            width: 100%;
            text-align: left;
            outline: none;
            transition: background-color 0.6s ease;
            display: flex;
            align-items: center;
        }

        .accordion-header:hover {
            background-color: #ccc;
        }

        .accordion-header.active {
            background-color: #ccc;
        }

        .accordion-header .icon {
            margin-right: 10px;
            font-size: 20px;
        }

        .accordion-content {
            padding: 0 18px;
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.2s ease-out;
            background-color: #fff;
        }

        .accordion-content p {
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="accordion" id="accordion">
        <div class="accordion-item">
            <button class="accordion-header" data-target="content1">
                <span class="icon">☰</span>
                选项一
            </button>
            <div class="accordion-content" id="content1">
                <p>这是选项一的内容。</p>
            </div>
        </div>
        <div class="accordion-item">
            <button class="accordion-header" data-target="content2">
                <span class="icon">☰</span>
                选项二
            </button>
            <div class="accordion-content" id="content2">
                <p>这是选项二的内容。</p>
            </div>
        </div>
        <div class="accordion-item">
            <button class="accordion-header" data-target="content3">
                <span class="icon">☰</span>
                选项三
            </button>
            <div class="accordion-content" id="content3">
                <p>这是选项三的内容。</p>
            </div>
        </div>
        <div class="accordion-item">
            <button class="accordion-header" data-target="content4">
                <span class="icon">☰</span>
                选项四
            </button>
            <div class="accordion-content" id="content4">
                <p>这是选项四的内容。</p>
            </div>
        </div>
    </div>

    <script>
        // 获取所有手风琴头部元素
        const accordionHeaders = document.querySelectorAll('.accordion-header');

        // 为每个手风琴头部添加点击事件
        accordionHeaders.forEach(header => {
            header.addEventListener('click', function() {
                // 获取目标内容的ID
                const targetId = this.getAttribute('data-target');
                const targetContent = document.getElementById(targetId);

                // 切换活动状态
                this.classList.toggle('active');

                // 切换内容显示
                if (targetContent.style.maxHeight) {
                    targetContent.style.maxHeight = null;
                } else {
                    targetContent.style.maxHeight = targetContent.scrollHeight + 'px';
                }

                // 关闭其他展开的内容
                accordionHeaders.forEach(otherHeader => {
                    if (otherHeader !== this) {
                        otherHeader.classList.remove('active');
                        const otherContent = document.getElementById(otherHeader.getAttribute('data-target'));
                        otherContent.style.maxHeight = null;
                    }
                });
            });
        });
    </script>
</body>
</html>